home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / mathstud.zip / TUTOR.M < prev    next >
Text File  |  1994-01-23  |  3KB  |  171 lines

  1. %This script M-file contain many of the examples in the
  2. %MathViews tutorial sector of the manual.
  3. %NOTE: MATHDEMO might not run all the examples (choose continue
  4. %if you get an error)
  5.  
  6. %       S.Halevy 7/31/92
  7. %       Copyright (c) 1992 by the MathWizards
  8.  
  9. clear
  10. %matrix and vector manipulations
  11. A = [ 1:4; 2:5]
  12. x = 2:-0.5:0.5
  13. x = sqrt(1:4)
  14. 1 + 1:5
  15. 1 + (1:5)
  16. 1:5 + 1
  17.  
  18. x = [ 1 2 3]
  19. x = [ x, 4 5 6]
  20. A = [ x; 5:10]
  21.  
  22. BA = [ 1 2 3 4 5 6 7 8 9]
  23. BA = [BA;BA;BA]
  24. BA = [BA;BA;BA]
  25. BAT = BA'
  26.  
  27. BA(1:4, 1:4)
  28. BA(3,:)
  29. x = [ 2 4 6]
  30. BA(:, x) = BA(:,6:8)
  31. BA(:,2:3) = BA(:, 3:-1:2)
  32. BA(2:3, [1 1 2 2])
  33.  
  34. L = [ 0 1 0 1 0 1 0 1 0]
  35. BA(L,:)
  36.  
  37. %matrix reshaping and assignment
  38. A = [ 1 2 3; 2 3 4]
  39. A(:)
  40. A(:) = 21:26
  41. A(:) = [ 21 24 26 3 4 5]
  42. %B(:) = 23:30 % should report an error
  43.  
  44. %linear and circular shifting.
  45. A = [ 1 2 3 4; 5 6 7 8; 9 10 11 ...
  46. 12; 13 14 15 16]
  47. A >> 2
  48. A >> -2
  49. A >> [ 0 2]
  50. A >> [ 2 -2]
  51.  
  52. A <> [ 2 -2 3]
  53. A <> [ 2 1 1; -2 3 4]
  54.  
  55. %matrix, element, column and vector deletion
  56. %using  [ ] matrix to delete rows/columns
  57. BA
  58. BA(:, [ 1 3 5]) = [ ]
  59. BA([2 4 6 8],:) = [ ]
  60.  
  61. %manipulation using logical vectors.
  62. %9/14 doesn't work yet
  63. %9/21 -- fixed
  64. A = [ 1 2 3 4; 5 6 7 8; 9 10 11 12]
  65. x = [ 1 0 1 0]
  66. y = [ 0 1 0]
  67. A(y,x)
  68.  
  69. x = [ 1 1 1 1]
  70. A(y,x)
  71.  
  72. x = [ 1 1 3]
  73. A(:,x)
  74.  
  75.  
  76.  
  77. %piping
  78. x = [ 1 2 3]
  79. abs( sqrt( cos( exp(x))))
  80. exp(x)->cos->sqrt->abs
  81.  
  82. %dependence assignment
  83. x = 1
  84. y = 1
  85. z = 2
  86. A := abs(x + y)
  87. B := sqrt(z + A)
  88.  
  89. %relational and logical operations
  90. %relational
  91. A =[ 1 2 3 4; 5 6 7 8; 9 10 11 12]
  92. x = (A >= 5)
  93. B =[ 9 2 45 4; 4 6 8 8; 9 20 11 12]
  94. x = A == B
  95.  
  96. %logical
  97. A = [  1 0 1; 0 1 0]
  98. B = [ 1 1 1; 0 1 1]
  99.  
  100. %A AND B
  101. A & B
  102.  
  103. %A OR B
  104. A | B
  105.  
  106. %NOT
  107. ~(A == B)
  108.  
  109. %if constructions
  110. A = [  1 0 1; 0 1 0]
  111. B = [ 1 1 1; 0 1 1]
  112. C = [ 1 1 1; 0 1 1]
  113. if ( all(all(A == B)))
  114.     disp('MathViews')
  115. else
  116.     disp('B ~= A')
  117. end
  118.  
  119. if ( all(all(C == A)))
  120.     disp('C == A')
  121. elseif all(all(C == B))
  122.     disp('C == B')
  123. end
  124.  
  125. %while and for constructions
  126. A = [  1 0 1; 0 1 0]
  127. x = 0
  128. while( A(2) == 0 )
  129.     disp(x++)
  130.     if x == 5
  131.         A(2) = 1
  132.     end
  133. end
  134.  
  135. A = zeros(2)
  136. B = rand(A)
  137. while B > A
  138.     disp(B>A)
  139.     A = B
  140.     B = rand(2,2)
  141. end
  142.  
  143. BA = [ 1 2 3 4 5 6 7 8 9]
  144. BA = [BA;BA;BA]
  145. BA = [BA;BA;BA]
  146. [m, n] = size(BA)
  147.  
  148. for i = 1:m
  149.     for j = 1:n
  150.         if ( i ~= j)
  151.             BA(i,j) = BA(j,i);
  152.         end
  153.     end
  154. end
  155. BA
  156.  
  157. for i = 1:m
  158.     for j = 1:n
  159.             BA(i,j) = i * j;
  160.             if ( (i == 2) & (j ==2))
  161.                  break
  162.             elseif ( (i == 4) & (j == 4))
  163.                 break
  164.             elseif( (i==6) & (j==6))
  165.                 break
  166.             end
  167.         end
  168.     end
  169. end
  170. BA
  171.